<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>零库存订单记录 - 虚拟仓系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <!-- 统一导航栏 -->
    {% include 'includes/navbar.html' %}

    <div class="container mt-4">
        <h3>零库存订单记录</h3>
        <div class="row mb-4">
            <div class="col-md-6">
                <div class="input-group">
                    <select id="supplierFilter" class="form-select">
                        <option value="">所有供应商</option>
                    </select>
                    <select id="statusFilter" class="form-select">
                        <option value="">所有状态</option>
                        <option value="已创建">已创建</option>
                        <option value="已发货">已发货</option>
                        <option value="已完成">已完成</option>
                        <option value="已取消">已取消</option>
                    </select>
                    <button id="searchBtn" class="btn btn-primary">查询</button>
                </div>
            </div>
        </div>
        <div class="card">
            <div class="card-body">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>订单号</th>
                            <th>供应商</th>
                            <th>商品ID</th>
                            <th>商品名称</th>
                            <th>数量</th>
                            <th>创建时间</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="ordersTable"></tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- 更新订单状态弹窗 -->
    <div class="modal fade" id="statusModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">更新订单状态</h5>
                    <button class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="statusForm">
                        <input type="hidden" id="orderId">
                        <div class="mb-3">
                            <label>订单状态</label>
                            <select class="form-select" id="newStatus" required>
                                <option value="已创建">已创建</option>
                                <option value="已发货">已发货</option>
                                <option value="已完成">已完成</option>
                                <option value="已取消">已取消</option>
                            </select>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button id="submitStatus" class="btn btn-primary">确认更新</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 加载供应商列表（带/api前缀）
        const loadSuppliers = async () => {
            const res = await fetch('/api/supplier/list');
            const suppliers = await res.json();
            const selector = document.getElementById('supplierFilter');
            selector.innerHTML += suppliers.map(s => `<option value="${s.id}">${s.supplier_name}</option>`).join('');
        };

        // 加载订单记录（带/api前缀）
        const loadOrders = async () => {
            const supplierId = document.getElementById('supplierFilter').value;
            const status = document.getElementById('statusFilter').value;
            const res = await fetch(`/api/order/zero-stock/list?supplier_id=${encodeURIComponent(supplierId)}&status=${encodeURIComponent(status)}`);
            const orders = await res.json();
            const table = document.getElementById('ordersTable');
            table.innerHTML = orders.map(order => `
                <tr>
                    <td>${order.order_id}</td>
                    <td>${order.supplier_name}</td>
                    <td>${order.product_id}</td>
                    <td>${order.product_name}</td>
                    <td>${order.quantity}</td>
                    <td>${order.create_time}</td>
                    <td><span class="badge ${getStatusBadgeClass(order.status)}">${order.status}</span></td>
                    <td>
                        <button class="btn btn-sm btn-info" onclick="openStatusModal('${order.order_id}')">更新状态</button>
                    </td>
                </tr>
            `).join('');
        };

        // 状态徽章样式
        const getStatusBadgeClass = (status) => {
            switch(status) {
                case '已创建': return 'bg-primary';
                case '已发货': return 'bg-info';
                case '已完成': return 'bg-success';
                case '已取消': return 'bg-danger';
                default: return 'bg-secondary';
            }
        };

        // 打开状态更新弹窗
        const openStatusModal = (orderId) => {
            document.getElementById('orderId').value = orderId;
            const modal = new bootstrap.Modal(document.getElementById('statusModal'));
            modal.show();
        };

        // 提交状态更新（带/api前缀）
        document.getElementById('submitStatus').addEventListener('click', async () => {
            const orderId = document.getElementById('orderId').value;
            const status = document.getElementById('newStatus').value;
            const res = await fetch('/api/order/zero-stock/status', {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify({order_id: orderId, status: status})
            });
            const data = await res.json();
            alert(data.message);
            if (data.status === 'success') {
                document.getElementById('statusForm').reset();
                const modal = bootstrap.Modal.getInstance(document.getElementById('statusModal'));
                modal.hide();
                loadOrders();
            }
        });

        document.getElementById('searchBtn').addEventListener('click', loadOrders);
        document.getElementById('logout').addEventListener('click', () => window.location.href = '/login_page');
        window.onload = async () => {
            await loadSuppliers();
            loadOrders();
        };
    </script>
</body>
</html>